const websocket = {
    data() {
        return {
            facility: "",
            socket: {},
            facilityAll: []
        };
    },
    methods: {
        init() {
            if (typeof WebSocket === "undefined") {
                console.log("您的浏览器不支持socket");
            } else {
                // 实例化socket
                var ip = window.location.hostname + ":" + window.location.port;
                var path = "ws://192.168.0.183:9092/wsEntry";
                this.socket = new WebSocket(path);
                // 监听socket连接
                this.socket.onopen = this.open;
                // 监听socket错误信息
                this.socket.onerror = this.error;
                // 监听socket消息
                this.socket.onmessage = this.getMessage;
            }
        },
        open() {
            console.log("socket连接成功");
        },
        error() {
            console.log("连接错误");
        },
        getMessage(msg) {
            let data = JSON.parse(msg.data);
            this.setEquipmentobj(data);
        },
        facilityChange(val) {
            this.socket.send(
                JSON.stringify({
                    type: this.catalog.sortCode,
                    facility: val
                })
            );
        }
    },
    destroyed() {
        this.socket = null;
    },
    mounted() {
        this.init();
    }
};
export default websocket;
